package mcjunkin.gui;
import mcjunkin.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
	This is a JPanel that shows the progress (a current directory, and
	a status).
*/

public class ControlWidget extends JPanel implements ProgressListener{

	//dependent widgets
	private StatusWidget sw;

	//progress info
	private long progress, total;

	//swing elements
	private JTextField ignore, folder;
	private JProgressBar bar;
	private JLabel status;
	private JButton start, stop, save;
	
	//action callbacks
	private Runnable startAction, stopAction, saveAction;

	/** Constructor builds the panel. */
	public ControlWidget(StatusWidget sw){
		
		//store the status widget
		this.sw = sw;
		
		//initially no progress
		progress = total = 0;
		
		//initially there are no start or stop actions
		startAction = stopAction = null;

		//lay things out top to bottom		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		//add the ignore list
		JPanel formPanel = new JPanel();

		//formpanel uses a spring layout 
		SpringLayout layout = new SpringLayout();
		formPanel.setLayout(layout);

		//create start/stop buttons
		start = new JButton("Start");
		stop = new JButton("Stop");
		save = new JButton("Save");
		formPanel.add(save);
		formPanel.add(start);
		formPanel.add(stop);
		
		//add actionlisteners for start button
		start.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e){
				bar.setVisible(true);
				if(startAction != null){
					Thread thread = new Thread(startAction);
					try{ thread.start(); } catch(Exception ex){}
				}
			}
		});

		//add actionlisteners for stop button
		stop.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(stopAction != null){
					Thread thread = new Thread(stopAction);
					try{ thread.start(); } catch(Exception ex){}
				}
			}
		});

		//add actionlisteners for save button
		save.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(saveAction != null){
					Thread thread = new Thread(saveAction);
					try{ thread.start(); } catch(Exception ex){}
				}
			}
		});

		//create label and jtextfield for folder
		folder = new JTextField();
		folder.setMargin(new Insets(2, 2, 2, 2));
		folder.setMaximumSize(new Dimension(Integer.MAX_VALUE,30));
		JLabel folderLabel = new JLabel("Folder");
		formPanel.add(folderLabel);
		formPanel.add(folder);

		//create label and jtextfield for ignore list
		ignore = new JTextField();
		ignore.setMargin(new Insets(2, 2, 2, 2));
		ignore.setMaximumSize(new Dimension(Integer.MAX_VALUE,30));
		JLabel ignoreLabel = new JLabel("Ignore");
		formPanel.add(ignoreLabel);
		formPanel.add(ignore);

		//add a label for current status
		status = new JLabel("");
		formPanel.add(status);
		
		//set constraints for position of objects 
		//each constraint reads
		//   layout.putConstraint(TSIDE, targetElem, off, ASIDE, anchorElem);
		//   targetElem.TSIDE = anchorElem.ASIDE+off
		//
		//example
		//   layout.putConstraint(WEST, folderLabel, 10, WEST, formPanel);
		//   folderLabel.WEST = formPanel.WEST + 10
		
		
		//store the constants from SpringLayout in convenient variable names
		//to make the layout code easier to read
		String WEST=SpringLayout.WEST, NORTH=SpringLayout.NORTH, 
			EAST=SpringLayout.EAST, SOUTH=SpringLayout.SOUTH;

		//folder label
		//folderLabel.WEST=formPanel.WEST+10
		layout.putConstraint(WEST, folderLabel, 10, WEST, formPanel);
		//folderLabel.NORTH=formPanel.NORTH+10
		layout.putConstraint(NORTH, folderLabel, 12, NORTH, formPanel);

		//folder
		layout.putConstraint(WEST, folder, 10, EAST, ignoreLabel);
		layout.putConstraint(NORTH, folder, 10, NORTH, formPanel);

		//ignore label
		layout.putConstraint(WEST, ignoreLabel, 10, WEST, formPanel);
		layout.putConstraint(NORTH, ignoreLabel, 12, SOUTH, folder);

		//ignore
		layout.putConstraint(WEST, ignore, 0, WEST, folder); 
		layout.putConstraint(NORTH, ignore, 10, SOUTH, folder); 

		//ignore label
		layout.putConstraint(WEST, status, 10, WEST, formPanel);
		layout.putConstraint(NORTH, status, 12, SOUTH, ignore); 

		//start button
		layout.putConstraint(EAST, start, -10, EAST, formPanel); 
		layout.putConstraint(NORTH, start, 10, SOUTH, ignore); 

		//save button
		layout.putConstraint(EAST, save, -10, WEST, stop); 
		layout.putConstraint(NORTH, save, 10, SOUTH, ignore); 

		//stop button
		layout.putConstraint(EAST, stop, -10, WEST, start); 
		layout.putConstraint(NORTH, stop, 10, SOUTH, ignore); 

		//bottom corner of page
		layout.putConstraint(EAST, formPanel, 0, EAST, folder);	
		layout.putConstraint(EAST, ignore, 0, EAST, folder);	
		layout.putConstraint(SpringLayout.SOUTH, formPanel,0, SOUTH, start); 

		//add the top panel
		formPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //TLBR	
		this.add(formPanel);
	
		//make a progress bar
		bar = new JProgressBar(0,100);
		bar.setValue(0);		
		bar.setStringPainted(true);		
		bar.setBorder(BorderFactory.createEmptyBorder(10,20,3,30)); //TLBR
		bar.setVisible(false);
		this.add(bar);

		//tell the status widget to update
		sw.setMessage1("");		
		sw.setMessage2("");		
		sw.updateMemory();
		
		//temp
		//folder.setText("/h/b/");
	}
	
	/** Updates the progress display. */
	public void setProgress(long i){
		this.progress = i;
		updateBar();
	}

	/** Updates the total display. */
	public void setTotal(long i){
		this.total = i;
		updateBar();
	}
	
	//helper to update the bar
	private void updateBar(){
		double value = ((double)progress)/((double)total)*100.0;
		bar.setValue((int)value);
		bar.setStringPainted(true);
		bar.setString(Lib.readableFileSize(progress)+"/"+
			Lib.readableFileSize(total));
		sw.updateMemory();
	}
	

	/** Updates the folder status display. */
	public void setCurrentFolder(String s){
		status.setText("Entered ");
	}

	/** Updates the callback for when start is pressed. */
	public void setStartAction(Runnable r){
		this.startAction = r;
	}

	/** Updates the callback for when stop is pressed. */
	public void setStopAction(Runnable r){
		this.stopAction = r;
	}

	/** Updates the callback for when save is pressed. */
	public void setSaveAction(Runnable r){
		this.saveAction = r;
	}

	/** Retrieves the folder entered by the user. */
	public String getFolder(){
		return folder.getText();
	}

	/** Retrieves the ignore list entered by the user. */
	public String getIgnore(){
		return ignore.getText();
	}

}
